package com.tencent.compose.sample.mainpage.sectionItem

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.material.Button
import kotlinx.cinterop.ExperimentalForeignApi

import kotlinx.cinterop.*
import platform.posix.*

@Composable
@OptIn(ExperimentalForeignApi::class)
internal fun San() {
    Column(
        Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .padding(30.dp)
            .verticalScroll(rememberScrollState())
    ) {

        Button(onClick = {
            val p = nativeHeap.allocArray<ByteVar>(1)
            nativeHeap.free(p)
            nativeHeap.free(p)
        }) {
            Text("double-free Kt")
        }

        Button(onClick = {
            val p = nativeHeap.allocArray<ByteVar>(1)
            p[1024] = 0.toByte()
            nativeHeap.free(p)
        }) {
            Text("heap-buffer-overflow Kt")
        }

        Button(onClick = {
            val p = nativeHeap.allocArray<ByteVar>(1)
            nativeHeap.free(p)
            p!![0] = 0x7F.toByte()
        }) {
            Text("heap-use-after-free Kt")
        }

        Button(onClick = {
            memScoped {
                val p = allocArray<ByteVar>(1)!!
                nativeHeap.free(p)
            }
        }) {
            Text("attempt-free-nonallocated-memory Kt")
        }

        PlatformInteropButtons()
    }
}

@Composable
internal expect fun PlatformInteropButtons()
